# -*- coding: utf-8 -*-
import os

from docx.document import Document

from docx.text.paragraph import Paragraph
from docx.text.run import Run

from docx.shared import Inches, Pt, RGBColor

from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

from docx.oxml import parse_xml
from docx.oxml.ns import qn, nsdecls

from py_word import Word, py_dir_path, picture_path


def text_run_set_font(run: Run, english_font='Times New Roman', chinese_font='宋体', font_size=12, font_color=RGBColor(0, 0, 0)):
    # 西文字体
    run.font.name = english_font
    # 中文字体
    chinese_font = english_font if chinese_font is None else chinese_font
    run._element.rPr.rFonts.set(qn('w:eastAsia'), chinese_font)
    # 字体设置
    run.font.size = Pt(font_size)
    run.font.color.rgb = font_color

    pass


if __name__ == '__main__':
    document_path = os.path.join(py_dir_path, 'paragraph.docx')
    document: Document = Word(document_path)
    # 段落：一个document包含多个paragraph
    paragraph: Paragraph = document.paragraphs[0]
    print(paragraph.text)
    paragraph.add_run(text='paragraph.add_run')

    # 段落之前插入段落
    paragraph.insert_paragraph_before(text='paragraph.insert_paragraph_before')

    # 一个paragraph包含多个run
    run: Run = paragraph.runs[0]
    print(run.text)
    run.add_break()
    run.add_text(text='run.add_text')
    # 文本设置字体
    text_run_set_font(run=run, english_font='Times New Roman', chinese_font='宋体', font_size=12)

    run.add_tab()
    # 插入图片
    run.add_picture(picture_path)

    document.save(os.path.join(py_dir_path, 'word.docx'))

    pass
